package com.example.myapp;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ContactsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ContactsFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<Contact> contactList;
    private ContactsAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_contacts, container, false);

        // 初始化RecyclerView
        recyclerView = view.findViewById(R.id.contactsRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        // 模拟联系人数据
        contactList = new ArrayList<>();
        contactList.add(new Contact("用户1", "小张", "地址8", "1234567890", R.drawable.user_image));
        contactList.add(new Contact("用户2", "小李", "地址7", "2345678901", R.drawable.user_image));
        contactList.add(new Contact("用户3", "小王", "地址6", "3456789012", R.drawable.user_image));
        contactList.add(new Contact("用户4", "小明", "地址5", "4567890123", R.drawable.user_image));
        contactList.add(new Contact("用户6", "小刚", "地址4", "5678901234", R.drawable.user_image));
        contactList.add(new Contact("用户7", "小红", "地址3", "6789012345", R.drawable.user_image));
        contactList.add(new Contact("用户8", "小彤", "地址2", "7890123456", R.drawable.user_image));
        contactList.add(new Contact("用户9", "小赵", "地址1", "8901234567", R.drawable.user_image));

        // 设置RecyclerView的Adapter
        adapter = new ContactsAdapter(contactList, new ContactsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Contact contact) {
                // 点击项时传递数据并跳转到详情页面
                Intent intent = new Intent(getContext(), ContactDetailActivity.class);
                intent.putExtra("nickname", contact.getNickname());
                intent.putExtra("realName", contact.getRealName());
                intent.putExtra("address", contact.getAddress());
                intent.putExtra("phone", contact.getPhone());
                intent.putExtra("imageResId", contact.getImageResId());
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);

        return view;
    }

    // Contact数据模型
    public static class Contact {
        private String nickname;
        private String realName;
        private String address;
        private String phone;
        private int imageResId;

        public Contact(String nickname, String realName, String address, String phone, int imageResId) {
            this.nickname = nickname;
            this.realName = realName;
            this.address = address;
            this.phone = phone;
            this.imageResId = imageResId;
        }

        public String getNickname() {
            return nickname;
        }

        public String getRealName() {
            return realName;
        }

        public String getAddress() {
            return address;
        }

        public String getPhone() {
            return phone;
        }

        public int getImageResId() {
            return imageResId;
        }
    }

    // RecyclerView适配器
    public static class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
        private List<Contact> contacts;
        private OnItemClickListener listener;

        public ContactsAdapter(List<Contact> contacts, OnItemClickListener listener) {
            this.contacts = contacts;
            this.listener = listener;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            Contact contact = contacts.get(position);
            holder.nickname.setText(contact.getNickname());
            holder.realName.setText(contact.getRealName());
            holder.contactImage.setImageResource(contact.getImageResId());

            // 点击事件
            holder.itemView.setOnClickListener(v -> listener.onItemClick(contact));
        }

        @Override
        public int getItemCount() {
            return contacts.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView nickname;
            TextView realName;
            ImageView contactImage;

            public ViewHolder(View itemView) {
                super(itemView);
                nickname = itemView.findViewById(R.id.contactNickname);
                realName = itemView.findViewById(R.id.contactRealName);
                contactImage = itemView.findViewById(R.id.contactImage);
            }
        }

        public interface OnItemClickListener {
            void onItemClick(Contact contact);
        }
    }
}
